package com.qf.springcloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.springcloud.client.AirSearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/air")
@RefreshScope    // 让通过@Value加载的配置信息,可以动态刷新
public class AirCoreController {

    @Resource
    private EurekaClient eurekaClient;

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private AirSearchClient airSearchClient;

    @Value("${v}")
    private String v;

    @GetMapping("/version")
    public String version(){
        return v;
    }


    @GetMapping("/core/test")
    public String test(){
        System.out.println("core-test: " + Thread.currentThread().getName());
        // 访问 air-search 服务中的/air/search/test

        /**
         * 通过EurekaClient实现获取地址信息
        //1.通过eurekaClient获取air-search服务的信息
        InstanceInfo airSearchInfo = eurekaClient.getNextServerFromEureka("air-search", false);

        //2. 通过InstanceInfo获取服务的ip和端口
        String ipAddr = airSearchInfo.getIPAddr();
        int port = airSearchInfo.getPort();

        //3. 发起请求
        String result = restTemplate.getForObject("http://" + ipAddr + ":" + port + "/air/search/test", String.class);
        */

        /**
         * 通过Ribbon实现服务的调用
         *
         String result = restTemplate.getForObject("http://AIR-SEARCH/air/search/test", String.class);
         */

        // 通过feign访问air-search服务中的test资源
        String result = airSearchClient.test();

        //4. return
        return "air-core服务调用air-search服务获取到：" + result;
    }


    @PostMapping("/core/test1")
    @HystrixCommand(fallbackMethod = "test1Fallback",
        commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "5"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "20000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "20"),
        }
    )
    public String test1(Integer id, String name,String gender) throws InterruptedException {
        if(id == 1){
            int i = 1 / 0;
        }
        System.out.println("core-test1: " + Thread.currentThread().getName());
        String result = airSearchClient.test1(id, name, gender);
        return result;
    }

    public String test1Fallback(Integer id, String name,String gender){
        // 处理错误的业务逻辑代码
        return "airCoreController的test1方法，出现了异常！！！";
    }





    @GetMapping("/core/test2/{brand}/{color}")
    public String test2(@PathVariable String brand, @PathVariable String color){
        String result = airSearchClient.test2(brand, color);
        return result;
    }

    @PostMapping("/core/test3")
    public String test3(@RequestBody Map map){
        String result = airSearchClient.test3(map);
        return result;
    }

}
